<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$mid=intval($_RQ['mid'])?:$_SESSION['uid'];
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$mid,
	);	
	$item=pdo_get('jy_ailiao_member',$params);
	$member=$this->model('member')->get_member();
	if($_RQ['fworker'] && !$member['level_id']){
		exi('您不是VIP不能访问红娘个人页面！','error');
	}
	$item['avatar']=tomedia($item['avatar']);
	$item['images']=json_decode($item['images'],true);
	$item['birth']=json_decode($item['birth'],true);
	$item['jiguan']=json_decode($item['jiguan'],true);
	foreach($item['images'] as &$img){
		$img=tomedia($img);
	}
	$item['image']=$item['images'][0]?:$item['avatar'];
	$item['siimgs']=json_decode($item['siimgs'],true);
	foreach($item['siimgs'] as &$img){
		$img=tomedia($img);
	}
	$item['sextitle']=$this->sex[$item['sex']];
	$item['height']=$this->heights[$item['height']];
	$item['picsnum']=count($item['images']);
	$item['sipicsnum']=count($item['siimgs']);
	$current_year=date('Y');
	$item['age'] = $item['birth']['year']?$current_year - $item['birth']['year'] - 1:'**';
	$minzus=$this->model('model')->minzus();
	$zhiweis=$this->model('model')->zhiweis();
	$item['zhiweititle']=$zhiweis[$item['zhiwei']];
	$myinfo=array(
		array('id'=>$item['minzu'],'title'=>$minzus[$item['minzu']]),
		array('id'=>$item['jiguan']['province'],'title'=>$item['jiguan']['province']?'籍贯:'.$item['jiguan']['province']:'籍贯:***'),
		array('id'=>$item['xueli'],'title'=>$this->xuelis[$item['xueli']]),
		array('id'=>$item['zhiwei'],'title'=>$zhiweis[$item['zhiwei']]),
		array('id'=>$item['isjiehun'],'title'=>$this->jiehuns[$item['isjiehun']]),
		array('id'=>$item['shouru'],'title'=>'薪资'.$this->shourus[$item['shouru']]),
		array('id'=>$item['youboy'],'title'=>$this->youboy[$item['youboy']]),
		array('id'=>$item['wanboy'],'title'=>$this->wanboy[$item['wanboy']]),
		array('id'=>$item['goufang'],'title'=>$this->goufang[$item['goufang']]),
		array('id'=>$item['gouche'],'title'=>$this->gouche[$item['gouche']]),
		array('id'=>$item['weight'],'title'=>'体重'.$this->weights[$item['weight']]),
		array('id'=>$item['height'],'title'=>'身高'.$this->heights[$item['height']]),
		array('id'=>$item['tixing'],'title'=>'体型'.$this->tixing[$item['tixing']]),
		array('id'=>$item['xiyan'],'title'=>$this->xiyan[$item['xiyan']]),
		array('id'=>$item['hejiu'],'title'=>$this->hejiu[$item['hejiu']]),
		array('id'=>$item['jh_time'],'title'=>$this->jh_time[$item['jh_time']]),
		array('id'=>$item['xingzuo'],'title'=>$this->xingzuo[$item['xingzuo']]),
	);
	$item['myinfo']=$myinfo;
	$toitem=pdo_get('jy_ailiao_tomember',array('uniacid'=>$_SESSION['uniacid'],'memberid'=>$item['mid']));
	if($toitem['id']){
		$toitem['birth']=json_decode($toitem['birth'],true);
		$toitem['jiguan']=json_decode($toitem['jiguan'],true);
		$toinfo=array(
			array('id'=>$toitem['minzu'],'title'=>$minzus[$toitem['minzu']]),
			array('id'=>$toitem['agekey'],'title'=>$this->years[$toitem['agekey']]),
			array('id'=>$toitem['jiguan']['province'],'title'=>$toitem['jiguan']['province']?'籍贯:'.$toitem['jiguan']['province']:'籍贯:***'),
			array('id'=>$toitem['xueli'],'title'=>$this->xuelis[$toitem['xueli']]),
			array('id'=>$toitem['zhiwei'],'title'=>$zhiweis[$toitem['zhiwei']]),
			array('id'=>$toitem['isjiehun'],'title'=>$this->jiehuns[$toitem['isjiehun']]),
			array('id'=>$toitem['isjiehun'],'title'=>$this->jiehuns[$toitem['isjiehun']]),
			array('id'=>$toitem['shouru'],'title'=>'薪资'.$this->shourus[$toitem['shouru']]),
			array('id'=>$toitem['youboy'],'title'=>$this->youboy[$toitem['youboy']]),
			array('id'=>$toitem['wanboy'],'title'=>$this->wanboy[$toitem['wanboy']]),
			array('id'=>$toitem['goufang'],'title'=>$this->goufang[$toitem['goufang']]),
			array('id'=>$toitem['gouche'],'title'=>$this->gouche[$toitem['gouche']]),
			array('id'=>$toitem['weight'],'title'=>'体重'.$this->weights2[$toitem['weight']]),
			array('id'=>$toitem['height'],'title'=>'身高'.$this->heights2[$toitem['height']]),
			array('id'=>$toitem['tixing'],'title'=>'体型'.$this->tixing[$toitem['tixing']]),
			array('id'=>$toitem['xiyan'],'title'=>$this->xiyan[$toitem['xiyan']]),
			array('id'=>$toitem['hejiu'],'title'=>$this->hejiu[$toitem['hejiu']]),
			array('id'=>$toitem['jh_time'],'title'=>$this->jh_time[$toitem['jh_time']]),
			array('id'=>$toitem['xingzuo'],'title'=>$this->xingzuo[$toitem['xingzuo']]),
		);
	}
	$item['toinfo']=$toinfo;
	$isadd=pdo_get('jy_ailiao_likes',array('mid'=>$_SESSION['uid'],'tomid'=>$item['mid']));
	if($isadd['id']){
		$item['islike']=1;
	}else{
		$item['islike']=0;
	}
	$item['newsnum'] = pdo_count('jy_ailiao_newslog',array('simi'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
	$item['sivideonum']=pdo_count('jy_ailiao_newslog',array('simi'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
	$sharelink='jy_ailiao/index//ismerch=1&merchid=0&fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>$title?:'婚恋相亲',
			'desc'=>$desc?:'婚恋相亲，成就天下姻缘！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($logo),
	);
	$siimgorder=pdo_get('jy_ailiao_siimgorder',array('tomid'=>$item['mid'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$item['isbuysiimg']=$siimgorder['status'];
	if($item['isbuysiimg']){
		$minzus=$this->model('siimg')->add_siimlog($item['mid']);
	}
	$siimgorder=pdo_get('jy_ailiao_sivideoorder',array('tomid'=>$item['mid'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$item['isbuysivideo']=$siimgorder['status'];
	$member=$this->model('member')->get_member();
	if($member['cansimi']){
		$item['isbuysiimg']=$item['isbuysivideo']=1;
	}
	$item['wechatnew']=$item['wechat']?:'********';
	if($this->setting['wechat_hong'] && $item['worker_id']){
		$item['wechatnew']=pdo_getcolumn('jy_ailiao_worker',array('id'=>$item['worker_id'],'uniacid'=>$_SESSION['uniacid']),'wechat');
	}
	$return['liao_lookmsg']=$this->model('member')->looklog($item['mid']);
	$return['item']=$item;
	$return['siimg']=$this->plugin_setting('siimg');
	$return['ping']=$this->plugin_setting('ping');
	$return['pcd']=array();
	$return['params']['pid']=intval($_RQ['pid']);
	$return['params']['keyword']='';
	$tcp=$this->model('kefu')->gettcp(0,$item['mid']);
	$liping=pdo_getall('jy_ailiao_liping',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
	$return['liping']=$liping;
	if($this->plugin_exists('jy_videocall')){
		$return['videocallurl']=custom_url('jy_videocall/index//pluginname=jy_ailiao&tomid='.$item['mid'].'&videobi='.$return['pluginset']['liao_videobi'],true);
		if($_RQ['endcall']){
			$this->model('kefu')->videocall($item['mid'],1);
		}
	}
	$return['post']= array('tomid'=>0,'msg'=>'','tcpid'=>$tcp['id'],'workerid'=>$tcp['workerid'],'star'=>5);
	$return['time']=time();
	set_html('title',$title);
	exi($return);
}
if($_RQ['op']=='looksiimg'){
	$postdata=get_postdata();
	$member=$this->model('member')->get_member();
	if($member['siminum']<=0){
		exi('目前剩余查看私密照次数不够！','error');
	}
	$item=pdo_get('jy_ailiao_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$postdata['id']));
	$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'tomid'=>$item['mid'],
			'sn'=>get_ordersn('jy_ailiao_siimgorder','sn'),
			'price'=>0,
			'status'=>1,
			'createtime'=>TIMESTAMP,
	);
	pdo_insert('jy_ailiao_siimgorder',$insert);
	pdo_update('jy_ailiao_member',array('siminum -='=>1),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	exi('查看成功！');
}
if($_RQ['op']=='buysiimg'){
	$postdata=get_postdata();
	$item=pdo_get('jy_ailiao_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$postdata['id']));
	$siimg=$this->plugin_setting('siimg');
	$price=$siimg['price'];
	if($price>0){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'tomid'=>$item['mid'],
			'sn'=>get_ordersn('jy_ailiao_siimgorder','sn'),
			'price'=>$price,
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_ailiao_siimgorder',$insert);
		$id=pdo_insertid();
			$params=array(
					'id'=>$id,
					'title'=>'付费查看私房照',
					'fee'=>$price,
					'plugin'=>'jy_ailiao',
					'no_type'=>array('hdfk'),
					'back_url'=>'jy_ailiao/memberitem/payresult/',
					'success_url'=>'jy_ailiao/index/',
					'error_url'=>'jy_ailiao/index/',
			);
			$payid=cfc('pay')->create_pay_order($params);
			pdo_update('jy_ailiao_siimgorder',array('uniacid'=>$_SESSION['uniacid'],'payid'=>$payid),array('id'=>$id));
			$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
			);
			exi($res);
	}else{
		exi('付费失败，价格未设置！');
	}
}
if($_RQ['op']=='setpay'){
	$id = intval($_RQ['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_ailiao_siimgorder',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$res=array(
		'pay_id'=>$order['payid'],
		'order_id'=>$order['id'],
	);
	exi($res);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_ailiao_siimgorder',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	$siimg=$this->plugin_setting('siimg');
	$rate=intval($siimg['rate']);
	$ratefee=$rate*$order['price']/100;
	pdo_update('jy_ailiao_siimgorder',array('status'=>1,'ratefee'=>$ratefee,'istixian'=>1,'paytype'=>$payresult['pay_type'],'transid'=>$payresult['transaction_id']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$payresult['id']));
	$money=$order['price']-$ratefee;
	cfc('core')->member_credit($order['mid'],'credit2',$money,true,'jy_ailiao','付费查看私房照');
	exi('支付成功','success','jy_ailiao/index/');
}
?>